use strict;

use lib '/var/www/perl/galleri';

use Galleri;
my $g = new Galleri();

my $cgi = new CGI;

my $pjx = $g->pjx;
$pjx->register( jsSendNewPass => \&jsSendNewPass );
$pjx->register( jsSetNewPass => \&jsSetNewPass );

my $code_ok;

if( my $code = $cgi->param( 'code' ) ){
  
  my ($check) = $g->check_code( $code );
  
  if( my $error = $check->{error} ){
    $g->add_content
      (
       subject => "Ugyldig kode",
       body => [ "Koden angitt er ugyldig." ],
      );
  }
  elsif( my $username = $check->{username} ){
    
    $code_ok = 1;
    
    $g->add_content
      (
       subject => "Sett Nytt Passord",
       body => 
       [ 
	qq{<div id="newpass">
          Sett nytt passord for <span style="color:blue">$username</span> her:
	  <form onsubmit="return false">
	  <input id="code" type="hidden" value="$code">
	  <table style="border:0;">
	  <tr><td>Nytt passord:</td><td><input id="passord1" type="password"></td></tr>
	  <tr><td>Gjenta passord:</td><td><input id="passord2" type="password"></td></tr>
	  <tr><td colspan="2" align="right"><input style="width:200px" type="button" value="Oppdater" onclick="document.getElementById('messagebox').innerHTML=''; jsSetNewPass(['passord1','passord2','code'],[result_of_newpass])"></td></tr>
	  </table>
	  </form>
	  </div>
	  <div id="messagebox"></div>
<script language="javascript">
function result_of_newpass(input){
  
  errors = Array();
  
  result = null;
  
  pairs = input.split("&");
  for( i=0; i<pairs.length; i++ ){
    nv = pairs[i].split("=");
    n=nv[0];
    v=nv[1];
    
    if(n=="error")
      errors[ errors.length ] = unescape(v);
    if(n=="result")
      result=v;
    
  }
  
  m = document.getElementById("messagebox");
  
  
  if(errors.length > 0){
    m.style.color = "red";
    m.innerHTML = errors.join("<br/>\\n");
  }
  else if(result){
    m.style.color = "blue";
    m.innerHTML = "Passordendring OK";
    document.getElementById("passord1").value = null;
    document.getElementById("passord2").value = null;
    d = document.getElementById("newpass");
    d.innerHTML = '';
  }
  
}
</script>

	 },
	
       ],
       
      )
    
  }
  
}
unless( $code_ok ){
  $g->add_content
    (
     subject => "Glemt Passord",
     body => [ q{<br/>Fyll inn brukernavn eller mailadresse her: } ,
	       q{<form name="newpassword" onsubmit="return false">
	     <input size="50" id="useroremail"> <input type="button" value="Send" onclick="document.getElementById('messagebox').innerHTML=''; jsSendNewPass(['useroremail'],[newpass_results])">
	     </form>
<div id="messagebox"></div>
<script language="javascript">
function newpass_results(input){
  
  errors = Array();
  
  recipent = null;

  pairs = input.split("&");
  for( i=0; i<pairs.length; i++ ){
    nv = pairs[i].split("=");
    n=nv[0];
    v=nv[1];
    
    if(n=="error")
      errors[ errors.length ] = unescape(v);
    if(n=="recipent")
      recipent=v;
    
  }
  
  m = document.getElementById("messagebox");
  
  if(errors.length > 0){
    m.style.color = "red";
    m.innerHTML = errors.join("<br/>\\n");
  }
  else if(recipent){
    m.style.color = "blue";
    m.innerHTML = "Mail har blitt sentd til " + recipent + ".";
    document.getElementById("useroremail").value = null;
  }
  
}
</script>
            },
	     ],
    );
  
}

sub jsSendNewPass {
  $g->send_new_pass(@_);
}

sub jsSetNewPass {
  $g->set_new_password(@_);
}

$g->output;

